<?php
/**
 * Generic form building class
 *
 * Various utilities for construcing, validating and displaying both for fields
 * and the results of form submissions.
 *
 * @category	ToolsAndUtilities
 * @package		textCleaner
 * @author		Anthony Willis <antwillis@gmail.com>
 * @copyright	2009 Anthony Willis
 * @license		http://www.gnu.org/licenses/gpl-1.0.html
 * @version		Release: 0.1
 * @link		http://code.google.com/p/phptextcleaner
 * @since		0.1
 * @todo		Only partially built. Still requires a number of additional
 * 				methods and refining and/or consolidation of existing methods.	
 */
class form {

    /**
     * Sets the request method
     *
     * @return void 
     */
	public function __construct($_request_method) {
		$this->request_method = $_request_method;
	}
    /**
     * Set a variable to the value of a form field, a cookie value or a passed
     * in default value in that order of preference and return the determined
     * value.
     *
     * @param string  $_name			Name of form field
     * @param string  $_default			Default value for variable
     * @param boolean $_set_cookie		Optionally set a cookie on form request
     *
     * @return	string	returns the value to assign the variable
     */
	public function set_variable($_name, $_set_cookie = true, $_initial = '') {

		if ($this->request_method == 'POST') {
			$variable =
				isSet($_POST[$_name]) ? $_POST[$_name] : '';
		} else {
			if (isSet($_GET[$_name])) {
				$variable = isSet($_GET[$_name]) ? $_GET[$_name] : '';
			} else {
				$variable = $_initial;
				if (isSet($_COOKIE[$_name]) && strlen($_COOKIE[$_name]) > 0) {
					$variable = $_COOKIE[$_name];
				}
			}
		}
		
		if ($_set_cookie) {
			setcookie($_name, $variable, time()+94608000);
		}
		return $variable;
	
	}
    /**
     * Sets the selected field state in a form field
     *
     * @param string	$_field			Name of form field
     * @param boolean	$_textfield		Determines if the field is a text type
     * @param string	$_expected		The field value to match
     * @param string	$_attrtype		The name of the attribute to write out
     * @param string	$_initial		The pre-defined value prior to form
     * 									submission
     *
     * @return	string	Returns the attribute to denote a matched field value or
     * 					the text value if the field is a textfield.
     */
	public function set_field(
			$_field, $_textfield = true, $_expected = '', $_attrtype = '',
			$_initial = '') {
		
		$_value = $this->set_variable($_field, false, $_initial);

		if ($_textfield) {
			return $_value;
		} else {
			return ($_value == $_expected)
				? $output = " " . $_attrtype . "=\"" . $_attrtype . "\"" : '';
		}

	}
    /**
     * Builds a select list
     *
     * @param string	$_name		Name of form field
     * @param array		$_options	Associative array of options with key as
     * 								option value and value as display value. 
     * @param array		$_label		Optional label value for the select list.
     * 								Associative array in the form;
     * 								array('value' => 'Label title', 'attr' =>
     * 										[array of attributes]) 
     * @param array		$_att		Associative array of attributes to place
     * 								within the select tag.
     * 								Attribute name and value key => value pairs.
     *
     * @return	string	Returns the constructed select list with optional label.
     */
	public function select(
		$_name, $_options, $_label = array(), $_att = array()) {
		
		$output = '';
		
		if (count($_options) > 0) {
		
			$label_value = (isset($_label['value'])) ? $_label['value'] : '';
			$label_attr = (isset($_label['attr'])) ? $_label['attr'] : '';
			
			$output .= $this->field_label($_label);
			
			$output .= "<select name=\"" . $_name . "\""
				. $this->write_attributes($_att) . ">";
				
			foreach($_options as $value => $display) {
				$status = $this->set_field($_name, false, $value, 'selected');
				$output .= "<option value=\"" . $value . "\"" . $status . ">";
				$output .= ($display) ? $display : $value . "</option>";
			}
			
			$output .= "</select>";
		}
		return $output;
	}
    /**
     * Builds single or grouped checkboxes
     *
     * @param string	$_name		Name of form field
     * @param array		$_values	Associative array of values with key as the
     * 								field value as an array containing label
     * 								information (value and attr keys) and an
     * 								array of field attributes in key => value
     * 								pairs. 
     * @param array		$_separator	Optional string to place between input tags.
     * 								This could be a space, or break tags, or any
     * 								valid string. 
     *
     * @return	string	Returns the constructed checkboxes with optional labels.
     */
	public function checkbox(
		$_name, $_values, $_separator = '') {
		$output = '';
		if (count($_values) > 0) {
			foreach($_values as $value => $display) {
				$status =
					$this->set_field(
						$_name,
						false,
						$value,
						'checked', 
						$display['initial_value']
					);
				$output .= $this->field_label($display['label']);
				$output .= "<input type=\"checkbox\" name=\"" . $_name;
				$output .= "\" value=\"" . $value . "\"";
				$output .= $this->write_attributes($display['attr']);
				$output .= $status . " /> ";
				$output .= $_separator;
			}
		}
		return $output;
	}
    /**
     * Builds an input field of type text
     *
     * @param string	$_name		Name of form field
     * @param array		$_values	Associative array of value and attributes. 
     * @param array		$_label		Optional label value for the text field.
     * 								Associative array in the form;
     * 								array('value' => 'Label title', 'attr' =>
     * 										[array of attributes]) 
     *
     * @return	string	Returns the constructed text field with optional label.
     */
	public function textfield(
		$_name, $_values = array(), $_label = array()) {
		
		$field_attr = (isset($_values['attr'])) ? $_values['attr'] : '';
		
		$output = '';
		$output .= $this->field_label($_label);
		$output .= "<input type=\"text\" name=\"" . $_name . "\" ";
		$output .= "value=\"" . $this->set_field($_name) . "\"";
		$output .= $this->write_attributes($field_attr) . " />";
		
		return $output;
	}
    /**
     * Builds a textarea field
     *
     * @param string	$_name		Name of form field
     * @param string	$_value		Value contained within textarea.
     * @param array		$_att		Associative array of attributes to place
     * 								within the textarea.
     * 								Attribute name and value key => value pairs.
     * @param array		$_label		Optional label value for the text field.
     * 								Associative array in the form;
     * 								array('value' => 'Label title', 'attr' =>
     * 										[array of attributes]) 
     *
     * @return	string	Returns the constructed textarea with optional label.
     */
	public function textarea(
		$_name, $_value = '', $_attr = array(), $_label = array()) {
		
		$field_attr = (isset($_values['attr'])) ? $_values['attr'] : '';

		$output = $this->field_label($_label);
		$output .= "<textarea name=\"" . $_name . "\"";
		$output .= $this->write_attributes($_attr) . ">";
		$output .= $_value . "</textarea>";
		
		return $output;
	}
    /**
     * Builds a submit button
     *
     * @param string	$_name		Name of submit button
     * @param string	$_value		Value to display on button.
     * @param array		$_att		Associative array of attributes to place
     * 								within the textarea.
     * 								Attribute name and value key => value pairs.
     *
     * @return	string	Returns the constructed textarea with optional label.
     */
	public function submit($_name, $_value = '', $_attr = array()) {
		$submit_value = ($_value) ? $_value : "submit";
		$output = "<input type=\"submit\" name=\"" . $_name . "\" ";
		$output .= "value=\"" . $submit_value . "\""; 
		$output .= $this->write_attributes($_attr) . " />";
		return $output;
	}
    /**
     * Constructs a field label
     *
     * @param array		$_label		Associative array in the form;
     * 								array('value' => 'Label title', 'attr' =>
     * 										[array of attributes]) 
     *
     * @return	string	Returns the constructed label.
     */
	public function field_label($_label) {
		$output = '';
		
		if (is_array($_label)) {
			$label_value = (isset($_label['value'])) ? $_label['value'] : '';
			$label_attr = (isset($_label['attr'])) ? $_label['attr'] : '';
			
			if (isset($_label['value'])) {
				$output .=
					"<label" . $this->write_attributes($label_attr) . ">";
				$output .= $label_value . " </label>";
			}
		}
		
		return $output;	
	}
	
    /**
     * Generates a string of HTML attributes in key => value pairs
     *
     * @param array	$_attributes	Associative array in the form;
     * 								'attribute name' => 'attribute value'
     *
     * @return	string	Returns the constructed string of HTML attributes.
     */
	private function write_attributes($_attributes) {
	
		$output = '';
		if (is_array($_attributes)) {
			foreach($_attributes as $key => $value) {
				$output .= " " . $key . "=\"" . $value . "\"";
			}
		}
		
		return $output;
	}

}

?>